<?php
namespace app\adminapiv3\controller\platform_manage;

/**
 * @name: 服务器配置接口
 * @Param: 
 * @Description: 
 * @Author: foo
 * @Date: 2019-10-23 13:36:31
 */
use app\adminapiv3\controller\BaseController;
use app\adminapiv3\service;
use app\common\service\StringHelper;

class ConfigController extends BaseController
{
    //配置类型->功能名称映射
    const logNameMap = [
        'login' => '登录设置',
        'ipWhiteSetting' => '功能设置',
        'syslog' => 'syslog配置'
        //..
    ];

    /**
     * IP白名单配置读取
    */
    public function getIpWhiteSetting(){

        $server = new service\platform_manage\ConfigService();
        $data = $server->getIpWhiteSetting();

        return $this->jsonOutPut($data);
    }

    /**
     * IP白名单配置写入
    */
    public function modifyIpWhiteSetting(){
        return $this->modify('ipWhiteSetting');
    }

    /**
     * syslog配置读取
    */
    public function getSyslogSetting(){
        return $this->get('syslog');
    }

    /**
     * syslog配置写入
     */
    public function modifySyslogSetting(){
        return $this->modify('syslog');
    }

    /**
     * 平台设置读取
    */
    public function getWebPageAlertsManage(){
        return $this->get('webPageAlertsManage');
    }

    /**
     * 平台设置写入
     */
    public function modifyWebPageAlertsManage(){
        return $this->modify('webPageAlertsManage');
    }

    /**
     * 登录设置读取
     */
    public function getLogin(){
        return $this->get('login');
    }

    /**
     * 登录设置写入
     */
    public function modifyLogin(){
        return $this->modify('login');
    }

    /**
     * 服务器运行状态查询
    */
    public function getServerRunningStatus(){
        return $this->get('serverRunningStatus');
    }

    /**
     * 检测是否已经安装
    */
    public function getCheckInstall(){
        return $this->get('checkInstall');
    }

    /**
     * 授权信息
    */
    public function getAuthorizationInformation(){
        return $this->get('authorizationInformation');
    }

    /**
     * elk配置查询
    */
    public function getElk(){
        return $this->get('elk');
    }

    /**
     * elk配置写入
     */
    public function modifyElk(){
        return $this->modify('elk');
    }

    /**
     * @name: 客户端获取配置
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-20 16:28:09
     */
    protected function get ($configType)
    {
        $data = $this->readConfig($configType);
        return $this->jsonOutPut($data);
    }

    /**
     * 读取配置数据
    */
    protected function readConfig($configType){
        $Config = new service\platform_manage\ConfigService();
        $this->toDo([$Config, 'checkConfigType'], [$configType]);
        return $this->toDo([$Config, 'configFget'], [$configType]);
    }

    /**
     * @name: 客户端更新配置
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-20 16:28:20
     */
    protected function modify ($configType)
    {
        //记录操作日志
        if (isset(self::logNameMap[$configType])) {
            global $logArray;
            $logArray = array(
                'content' => self::logNameMap[$configType]
            );
        }
        $Config = new service\platform_manage\ConfigService();
        $fieldData = $this->toDo([$Config, 'checkConfigType'], [$configType]);

        $updateData = [];

        //记录操作日志
        $logData = [
            'new_info' => '',
            'old_info' => ''
        ];
        //查询原配置数据
        $oldData = $this->readConfig($configType);
        $isLog = false;

        foreach ($fieldData as $k => $v)
        {
            if($configType == 'webPageAlertsManage'){
                $updateData[$v] = $this->filterInput($v, 'post');//接受参数
            }else{
                $updateData[$v] = $this->filterInput($v.'*', 'post');//接受参数
            }

            //特殊类型字段映射 方法名称固定为logParam + 配置key(大驼峰) + 参数名称大驼峰
            //$Config->方法名称
            //记录日志
            if(!is_int($k)){
                $method = 'logParam' . ucfirst(StringHelper::convertUnderline($configType))  . ucfirst(StringHelper::convertUnderline($v));
                $isTrans = method_exists($Config,$method);
                if($isTrans){
                    $logData['new_info'] .= $k . '：' . $Config->$method($updateData[$v]) .'；';
                    $logData['old_info'] .= $k . '：' . $Config->$method($oldData[$v]).'；';
                }else{
                    $logData['new_info'] .= $k . '：' .$updateData[$v] .'；';
                    $logData['old_info'] .= $k . '：' . $oldData[$v].'；';
                }
                $isLog = true;
            }
        }
        if($isLog){
            $logArray['new_info'] = $logData['new_info'];
            $logArray['old_info'] = $logData['old_info'];
        }


        $this->toDo([$Config, 'configFwrite'], [$updateData]);
        return $this->jsonOutPut();
    }
}